

package com.hazelcast.query.impl.getters;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public final class MethodGetter extends AbstractMultiValueGetter {

    private final Method method;

    // for testing purposes only
    public MethodGetter(Getter parent, Method method, String modifier, Class elementType) {
        this(parent, method, modifier, method.getReturnType(), elementType);
    }

    public MethodGetter(Getter parent, Method method, String modifier, Class type, Class elementType) {
        super(parent, modifier, type, elementType);
        this.method = method;
    }

    @Override
    protected Object extractFrom(Object object) throws IllegalAccessException, InvocationTargetException {
        try {
            return method.invoke(object);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException(composeAttributeValueExtractionFailedMessage(method), e);
        }
    }

    @Override
    boolean isCacheable() {
        return true;
    }

    @Override
    public String toString() {
        return "MethodGetter [parent=" + parent + ", method=" + method.getName() + ", modifier = " + getModifier() + "]";
    }

}
